import absmaincontroler from './absmaincontroler.js'

import commonMainModify from './common/modifymaincontroler.js'
import { FastToolPanelSvc } from '../service/fasttoolpanelsvc.js'
import LGXCOM from '../common/common.js'
import { LGXEnums } from '../common/sysenum.js'

class VisoMainControler extends absmaincontroler.AbsMainControler {
    constructor(manager, displayMng, mulselsvc) {
        super(manager, displayMng, mulselsvc);
        this.mainModdifyControler = new commonMainModify.ModifyMainControler(this, this.dataManger, mulselsvc);
        this.curentControler = this.mainModdifyControler;
        this.fastToolPanelSvc = new FastToolPanelSvc(this.dataManger);
    }

    act2Mouse(painter, mosEvnType, preWorldPt) {
        this.dataManger.clearFreshZone();
        var validPt = new LGXCOM.LGXPoint();
        validPt.clone(preWorldPt);

        let preFinish = 0;
        let preCurentGraph = this.dataManger.getCurentGraph();
        if (preCurentGraph) {
            preFinish = preCurentGraph.getFinish();
        }

        let mode = this.dataManger.getMapWorkMode();
        let resultState = null;
        if (mosEvnType.type == LGXEnums.LGXMouseMode.LMouseDown
            && mode == LGXEnums.LGXWorkMode.ModifyMode
        ) {
            resultState = this.fastToolPanelSvc.actMouseEvent(mosEvnType, preWorldPt);
        }
        let fastDrawRet = false;
        if (resultState && resultState.action == "fast_draw" && resultState.tool != 0) {
            fastDrawRet = true;
        }
        if (!fastDrawRet) {
            resultState = this.curentControler.act2Mouse(mosEvnType, validPt, painter);
        }
        preWorldPt.x = validPt.x;
        preWorldPt.y = validPt.y;
        let pCurentGraph = this.dataManger.getCurentGraph();

        if (mode == LGXEnums.LGXWorkMode.DrawGraphMode) {
            if (pCurentGraph && pCurentGraph.getFinish() == 1) {
                if (pCurentGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
                    console.log();
                }
                else if (preFinish == 0 || pCurentGraph != preCurentGraph) {
                    this.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
                }
            }
        }

        if (mosEvnType.type == LGXEnums.LGXMouseMode.LMouseDown) {
            this.hideAbsorbRayLine(validPt, 1);
            this.hideAbsorbRayLine(validPt, 2);
        }

        if (mosEvnType.type == LGXEnums.LGXMouseMode.mouseMove && mode == LGXEnums.LGXWorkMode.ModifyMode) {
            resultState = this.fastToolPanelSvc.actMouseEvent(mosEvnType, preWorldPt);
        }
        this.rePaint(painter, 0);
        return resultState;
    }

    actToMouseHover(painter, mousePoint) {
        if (this.dataManger.getMapWorkMode() == LGXEnums.LGXWorkMode.ModifyMode) {
            let ret = this.fastToolPanelSvc.actToMouseHover(mousePoint);
            this.rePaint(painter, 0);
        }
    }

    setMapWorkMode(mode) {
        this.dataManger.setMapWorkMode(mode);
        if (mode == LGXEnums.LGXWorkMode.DrawGraphMode) {
            this.curentControler = this.plotControler;
        }
        else if (mode == LGXEnums.LGXWorkMode.ModifyMode) {
            this.curentControler = this.mainModdifyControler;
        }
        else if (mode == LGXEnums.LGXWorkMode.WanderMode) {
            this.curentControler = this.panViewControler;
        }
        this.fastToolPanelSvc.hideFastPanel();
    }

    hideAbsorbRayLine(point, kind) {
        let auxDataMng = this.dataManger.getAuxDataManager();
        auxDataMng.resetProcessingEdgeDirLine(point, point, kind, 2, null, null);
        let ptemp = this.dataManger.getYLastAbsorbGraph();
        if (ptemp && ptemp.getUseful() == 1) {
            this.dataManger.recordRefreshScope(ptemp.getZone());
        }

        let graphZoneLst = this.dataManger.setAbsorbRayLine(kind, null);
        for (let i = 0; i < graphZoneLst.length; i++) {
            let rect = graphZoneLst[i];
            this.dataManger.recordRefreshScope(rect);
        }
    }

    notifyGenerateFastGraph(painter) {
        this.fastToolPanelSvc.notifyGenerateFastGraph();
        this.rePaint(painter, 0);
    }

    rePaint(map, kind) {
        this.displayMng.rePaint(map, this.dataManger, kind);
        this.fastToolPanelSvc.display(map, this.displayMng, this.dataManger.getMapViewInfo());
    }

    getCursorBySelGraphKind(painter, mosEvent, screenPt, mousePt) {
        let cursor = "auto";
        if (this.fastToolPanelSvc.chkFastToolPanelShow()) {
            return cursor;
        }
        let curentSelGraph = this.dataManger.getCurentGraph();
        if (!curentSelGraph) {
            return cursor;
        }


        let showCustCursor = false;
        let mapViewInfo = this.dataManger.getMapViewInfo();
        let preShowCustCursor = mapViewInfo.getShowCustCursor();

        let scaling = this.dataManger.getScaling();
        let mapscale = this.dataManger.getMapScale();
        let dragKind = curentSelGraph.calculateSelKind(mousePt, scaling, mapscale);
        switch (dragKind) {
            case LGXEnums.LGXModGraphKind.Drag_leftTop:
                cursor = "nw-resize";//north west
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_rightTop:
                cursor = "ne-resize"; //north east
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_RightBottom:
                cursor = "se-resize"; //south east
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_leftBottom:
                cursor = "sw-resize"; //south west
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_TopMid:
                cursor = "n-resize"; //north east
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_RightMid:
                cursor = "e-resize"; //east
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_BottomMid:
                cursor = "s-resize"; //south
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_LeftMid:
                cursor = "w-resize"; //west
                showCustCursor = true;
                break;

            case LGXEnums.LGXModGraphKind.Drag_center:
                cursor = "move"; //center
                break;

            case LGXEnums.LGXModGraphKind.Drag_rotHand:
                cursor = "pointer";
                break;
        }

        if (showCustCursor) {
            let sizeDragInfo = curentSelGraph.getSizeDragInfo(dragKind);
            mapViewInfo.setCustCursorPt(sizeDragInfo.pt);
            mapViewInfo.setCustCursorVector(sizeDragInfo.vector);
            cursor = "none";
        }
        mapViewInfo.setShowCustCursor(showCustCursor);
        if (preShowCustCursor != showCustCursor) {
            this.dataManger.recordRepaintScope(curentSelGraph);
            this.rePaint(painter, 0);
        }
        return cursor;
    }
}

export default { VisoMainControler }

